<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.10"/>
<title>Introduction_to_Algorithms: src/sort_algorithms/heap_sort/heapsort.h Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Introduction_to_Algorithms
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.10 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('heapsort_8h_source.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">heapsort.h</div>  </div>
</div><!--header-->
<div class="contents">
<a href="heapsort_8h.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="comment">/*</span></div>
<div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="comment"> * Copyright 2016- huaxz &lt;huaxz1986@163.com&gt;</span></div>
<div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="comment"> * This program is free software: you can redistribute it and/or modify</span></div>
<div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="comment"> * it under the terms of the GNU General Public License as published by</span></div>
<div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;<span class="comment"> * the Free Software Foundation, either version 2 of the License, or</span></div>
<div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;<span class="comment"> * (at your option) any later version.</span></div>
<div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;<span class="comment"> * This program is distributed in the hope that it will be useful,</span></div>
<div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;<span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span></div>
<div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;<span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span></div>
<div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;<span class="comment"> * GNU General Public License for more details.</span></div>
<div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;<span class="comment"> * You should have received a copy of the GNU General Public License</span></div>
<div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;<span class="comment"> * along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span></div>
<div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;<span class="comment"> * Author: huaxz1986@163.com (huaxz)</span></div>
<div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;<span class="preprocessor">#ifndef HEAP_SORT</span></div>
<div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;<span class="preprocessor">#define HEAP_SORT</span></div>
<div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;<span class="keyword">namespace </span><a class="code" href="namespace_introduction_to_algorithm.html">IntroductionToAlgorithm</a></div>
<div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;{</div>
<div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;    <span class="keyword">namespace </span>SortAlgorithm</div>
<div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;    {</div>
<div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;</div>
<div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;</div>
<div class="line"><a name="l00039"></a><span class="lineno">   39</span>&#160;        template&lt;typename Iterator,typename CompareType=std::less&lt;typename std::iterator_traits&lt;Iterator&gt;::value_type&gt;&gt;</div>
<div class="line"><a name="l00040"></a><span class="lineno"><a class="line" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html">   40</a></span>&#160;        <span class="keyword">class </span><a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html">Sort_Heap</a></div>
<div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;        {</div>
<div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;        <span class="keyword">public</span>:</div>
<div class="line"><a name="l00043"></a><span class="lineno"><a class="line" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a5af910656f5d3c65d47f992dbf380c61">   43</a></span>&#160;            <span class="keyword">typedef</span> <span class="keyword">typename</span> std::iterator_traits&lt;Iterator&gt;::value_type <a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a5af910656f5d3c65d47f992dbf380c61">T</a>; </div>
<div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;</div>
<div class="line"><a name="l00056"></a><span class="lineno"><a class="line" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#adfaf638a6fd27c6295f7ac0a07484a00">   56</a></span>&#160;            <span class="keywordtype">void</span> <a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#adfaf638a6fd27c6295f7ac0a07484a00">operator () </a>(<span class="keyword">const</span> Iterator from, std::size_t size,CompareType compare=CompareType())</div>
<div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;            {</div>
<div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;                <a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a484d28581bfe39a387d90852e38ac36c">_from</a>=from;</div>
<div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;                <a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a57a42830d3b52d60d2ec1dfd11e2bda6">_size</a>=size;</div>
<div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;                <a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a64c05fac6da6e6e0d63e733640d0cf97">_setupHeap</a>(compare);</div>
<div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;                <span class="keywordflow">while</span>(<a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a57a42830d3b52d60d2ec1dfd11e2bda6">_size</a>&gt;0)</div>
<div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;                {</div>
<div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;                    std::swap(*<a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a484d28581bfe39a387d90852e38ac36c">_from</a>,*(<a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a484d28581bfe39a387d90852e38ac36c">_from</a>+<a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a57a42830d3b52d60d2ec1dfd11e2bda6">_size</a>-1));</div>
<div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;                    <a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a57a42830d3b52d60d2ec1dfd11e2bda6">_size</a>--;</div>
<div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;                    <a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a0535910130c6c750faf7801059e4a9fa">_heapify</a>(0,compare);</div>
<div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;                }</div>
<div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;            }</div>
<div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;        <span class="keyword">protected</span>:</div>
<div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;</div>
<div class="line"><a name="l00079"></a><span class="lineno"><a class="line" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a64c05fac6da6e6e0d63e733640d0cf97">   79</a></span>&#160;            <span class="keywordtype">void</span> <a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a64c05fac6da6e6e0d63e733640d0cf97">_setupHeap</a>(CompareType compare=CompareType())</div>
<div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160;            {</div>
<div class="line"><a name="l00081"></a><span class="lineno">   81</span>&#160;                <span class="keywordflow">if</span>(<a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a57a42830d3b52d60d2ec1dfd11e2bda6">_size</a>&lt;=1)</div>
<div class="line"><a name="l00082"></a><span class="lineno">   82</span>&#160;                    <span class="keywordflow">return</span>;</div>
<div class="line"><a name="l00083"></a><span class="lineno">   83</span>&#160;                <span class="keywordtype">int</span> index=(<a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a57a42830d3b52d60d2ec1dfd11e2bda6">_size</a>-1)/2;</div>
<div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;                <span class="keywordflow">while</span>(index&gt;=0)</div>
<div class="line"><a name="l00085"></a><span class="lineno">   85</span>&#160;                {</div>
<div class="line"><a name="l00086"></a><span class="lineno">   86</span>&#160;                    <a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a0535910130c6c750faf7801059e4a9fa">_heapify</a>(index,compare);</div>
<div class="line"><a name="l00087"></a><span class="lineno">   87</span>&#160;                    index--;</div>
<div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160;                }</div>
<div class="line"><a name="l00089"></a><span class="lineno">   89</span>&#160;            }</div>
<div class="line"><a name="l00091"></a><span class="lineno">   91</span>&#160;</div>
<div class="line"><a name="l00102"></a><span class="lineno"><a class="line" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a0535910130c6c750faf7801059e4a9fa">  102</a></span>&#160;            <span class="keywordtype">void</span> <a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a0535910130c6c750faf7801059e4a9fa">_heapify</a>(std::size_t elementIndex,CompareType compare=CompareType())</div>
<div class="line"><a name="l00103"></a><span class="lineno">  103</span>&#160;            {</div>
<div class="line"><a name="l00104"></a><span class="lineno">  104</span>&#160;                <span class="keywordflow">if</span>(elementIndex&gt;=<a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a57a42830d3b52d60d2ec1dfd11e2bda6">_size</a>)</div>
<div class="line"><a name="l00105"></a><span class="lineno">  105</span>&#160;                    <span class="keywordflow">return</span>;</div>
<div class="line"><a name="l00106"></a><span class="lineno">  106</span>&#160;                <span class="keyword">auto</span> maxIndex=elementIndex;</div>
<div class="line"><a name="l00107"></a><span class="lineno">  107</span>&#160;                <span class="keywordtype">bool</span> left_valid=<span class="keyword">true</span>;</div>
<div class="line"><a name="l00108"></a><span class="lineno">  108</span>&#160;                <span class="keywordtype">bool</span> right_valid=<span class="keyword">true</span>;</div>
<div class="line"><a name="l00109"></a><span class="lineno">  109</span>&#160;                <span class="keyword">auto</span> leftIndex=<a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#af4033a3e0cd7dab78cb1081f3dece165">_lchildIndex</a>(elementIndex,left_valid);</div>
<div class="line"><a name="l00110"></a><span class="lineno">  110</span>&#160;                <span class="keyword">auto</span> rightIndex=<a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a71532dbfd31fdb854aa46dbca6e411ac">_rchildIndex</a>(elementIndex,right_valid);</div>
<div class="line"><a name="l00111"></a><span class="lineno">  111</span>&#160;</div>
<div class="line"><a name="l00112"></a><span class="lineno">  112</span>&#160;                <span class="keywordflow">if</span>(left_valid)</div>
<div class="line"><a name="l00113"></a><span class="lineno">  113</span>&#160;                {</div>
<div class="line"><a name="l00114"></a><span class="lineno">  114</span>&#160;                    <span class="keywordflow">if</span>( compare(*(<a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a484d28581bfe39a387d90852e38ac36c">_from</a>+maxIndex),*(<a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a484d28581bfe39a387d90852e38ac36c">_from</a>+leftIndex)))</div>
<div class="line"><a name="l00115"></a><span class="lineno">  115</span>&#160;                        maxIndex=leftIndex;</div>
<div class="line"><a name="l00116"></a><span class="lineno">  116</span>&#160;                }</div>
<div class="line"><a name="l00117"></a><span class="lineno">  117</span>&#160;                <span class="keywordflow">if</span>(right_valid)</div>
<div class="line"><a name="l00118"></a><span class="lineno">  118</span>&#160;                {</div>
<div class="line"><a name="l00119"></a><span class="lineno">  119</span>&#160;                    <span class="keywordflow">if</span>( compare(*(<a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a484d28581bfe39a387d90852e38ac36c">_from</a>+maxIndex),*(<a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a484d28581bfe39a387d90852e38ac36c">_from</a>+rightIndex)))</div>
<div class="line"><a name="l00120"></a><span class="lineno">  120</span>&#160;                        maxIndex=rightIndex;</div>
<div class="line"><a name="l00121"></a><span class="lineno">  121</span>&#160;                }</div>
<div class="line"><a name="l00122"></a><span class="lineno">  122</span>&#160;                <span class="keywordflow">if</span>(maxIndex!=elementIndex)</div>
<div class="line"><a name="l00123"></a><span class="lineno">  123</span>&#160;                {</div>
<div class="line"><a name="l00124"></a><span class="lineno">  124</span>&#160;                    std::swap(*(<a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a484d28581bfe39a387d90852e38ac36c">_from</a>+elementIndex),*(<a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a484d28581bfe39a387d90852e38ac36c">_from</a>+maxIndex));</div>
<div class="line"><a name="l00125"></a><span class="lineno">  125</span>&#160;                    <a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a0535910130c6c750faf7801059e4a9fa">_heapify</a>(maxIndex,compare);</div>
<div class="line"><a name="l00126"></a><span class="lineno">  126</span>&#160;                }</div>
<div class="line"><a name="l00127"></a><span class="lineno">  127</span>&#160;            }</div>
<div class="line"><a name="l00129"></a><span class="lineno">  129</span>&#160;</div>
<div class="line"><a name="l00139"></a><span class="lineno"><a class="line" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a72f5fa2e41901a80fdb9a3bcece8a33f">  139</a></span>&#160;            std::size_t <a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a72f5fa2e41901a80fdb9a3bcece8a33f">_parentIndex</a>(std::size_t elementIndex,<span class="keywordtype">bool</span>&amp; valid)</div>
<div class="line"><a name="l00140"></a><span class="lineno">  140</span>&#160;            {</div>
<div class="line"><a name="l00141"></a><span class="lineno">  141</span>&#160;                <span class="keywordflow">if</span>(elementIndex&gt;=<a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a57a42830d3b52d60d2ec1dfd11e2bda6">_size</a>)</div>
<div class="line"><a name="l00142"></a><span class="lineno">  142</span>&#160;                {</div>
<div class="line"><a name="l00143"></a><span class="lineno">  143</span>&#160;                    valid=<span class="keyword">false</span>; <span class="comment">//无效结果</span></div>
<div class="line"><a name="l00144"></a><span class="lineno">  144</span>&#160;                    <span class="keywordflow">return</span> 0;</div>
<div class="line"><a name="l00145"></a><span class="lineno">  145</span>&#160;                }</div>
<div class="line"><a name="l00146"></a><span class="lineno">  146</span>&#160;                valid=<span class="keyword">true</span>; <span class="comment">//有效结果</span></div>
<div class="line"><a name="l00147"></a><span class="lineno">  147</span>&#160;                <span class="keywordflow">if</span> (elementIndex ==0) <span class="keywordflow">return</span> 0; <span class="comment">//根节点的父节点是自己</span></div>
<div class="line"><a name="l00148"></a><span class="lineno">  148</span>&#160;                <span class="keywordflow">else</span>  <span class="keywordflow">return</span> (elementIndex-1)&gt;&gt;1;</div>
<div class="line"><a name="l00149"></a><span class="lineno">  149</span>&#160;            }</div>
<div class="line"><a name="l00151"></a><span class="lineno">  151</span>&#160;</div>
<div class="line"><a name="l00162"></a><span class="lineno"><a class="line" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#af4033a3e0cd7dab78cb1081f3dece165">  162</a></span>&#160;            std::size_t <a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#af4033a3e0cd7dab78cb1081f3dece165">_lchildIndex</a>(std::size_t elementIndex,<span class="keywordtype">bool</span>&amp; valid)</div>
<div class="line"><a name="l00163"></a><span class="lineno">  163</span>&#160;            {</div>
<div class="line"><a name="l00164"></a><span class="lineno">  164</span>&#160;                <span class="keywordflow">if</span>(<a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a57a42830d3b52d60d2ec1dfd11e2bda6">_size</a>&lt;2)</div>
<div class="line"><a name="l00165"></a><span class="lineno">  165</span>&#160;                {</div>
<div class="line"><a name="l00166"></a><span class="lineno">  166</span>&#160;                    valid=<span class="keyword">false</span>; <span class="comment">//数组元素太少无效结果</span></div>
<div class="line"><a name="l00167"></a><span class="lineno">  167</span>&#160;                    <span class="keywordflow">return</span> 0;</div>
<div class="line"><a name="l00168"></a><span class="lineno">  168</span>&#160;                }</div>
<div class="line"><a name="l00169"></a><span class="lineno">  169</span>&#160;                <span class="keywordflow">if</span>(  elementIndex&gt;((<a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a57a42830d3b52d60d2ec1dfd11e2bda6">_size</a>-2)&gt;&gt;1)  )</div>
<div class="line"><a name="l00170"></a><span class="lineno">  170</span>&#160;                {</div>
<div class="line"><a name="l00171"></a><span class="lineno">  171</span>&#160;                    valid=<span class="keyword">false</span>;<span class="comment">//超出范围，无效</span></div>
<div class="line"><a name="l00172"></a><span class="lineno">  172</span>&#160;                    <span class="keywordflow">return</span> 0;</div>
<div class="line"><a name="l00173"></a><span class="lineno">  173</span>&#160;                }</div>
<div class="line"><a name="l00174"></a><span class="lineno">  174</span>&#160;                valid=<span class="keyword">true</span>;</div>
<div class="line"><a name="l00175"></a><span class="lineno">  175</span>&#160;                <span class="keywordflow">return</span> (elementIndex&lt;&lt;1)+1;</div>
<div class="line"><a name="l00176"></a><span class="lineno">  176</span>&#160;            }</div>
<div class="line"><a name="l00177"></a><span class="lineno">  177</span>&#160;</div>
<div class="line"><a name="l00179"></a><span class="lineno">  179</span>&#160;</div>
<div class="line"><a name="l00190"></a><span class="lineno"><a class="line" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a71532dbfd31fdb854aa46dbca6e411ac">  190</a></span>&#160;            std::size_t <a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a71532dbfd31fdb854aa46dbca6e411ac">_rchildIndex</a>(std::size_t elementIndex,<span class="keywordtype">bool</span>&amp; valid)</div>
<div class="line"><a name="l00191"></a><span class="lineno">  191</span>&#160;            {</div>
<div class="line"><a name="l00192"></a><span class="lineno">  192</span>&#160;                <span class="keywordflow">if</span>(<a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a57a42830d3b52d60d2ec1dfd11e2bda6">_size</a>&lt;3)</div>
<div class="line"><a name="l00193"></a><span class="lineno">  193</span>&#160;                {</div>
<div class="line"><a name="l00194"></a><span class="lineno">  194</span>&#160;                    valid=<span class="keyword">false</span>; <span class="comment">//数组元素太少无效结果</span></div>
<div class="line"><a name="l00195"></a><span class="lineno">  195</span>&#160;                    <span class="keywordflow">return</span> 0;</div>
<div class="line"><a name="l00196"></a><span class="lineno">  196</span>&#160;                }</div>
<div class="line"><a name="l00197"></a><span class="lineno">  197</span>&#160;                <span class="keywordflow">if</span>(  elementIndex&gt;((<a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a57a42830d3b52d60d2ec1dfd11e2bda6">_size</a>-3)&gt;&gt;1)  )</div>
<div class="line"><a name="l00198"></a><span class="lineno">  198</span>&#160;                {</div>
<div class="line"><a name="l00199"></a><span class="lineno">  199</span>&#160;                    valid=<span class="keyword">false</span>;<span class="comment">//超出范围，无效</span></div>
<div class="line"><a name="l00200"></a><span class="lineno">  200</span>&#160;                    <span class="keywordflow">return</span> 0;</div>
<div class="line"><a name="l00201"></a><span class="lineno">  201</span>&#160;                }</div>
<div class="line"><a name="l00202"></a><span class="lineno">  202</span>&#160;                valid=<span class="keyword">true</span>;</div>
<div class="line"><a name="l00203"></a><span class="lineno">  203</span>&#160;                <span class="keywordflow">return</span> (elementIndex&lt;&lt;1)+2;</div>
<div class="line"><a name="l00204"></a><span class="lineno">  204</span>&#160;            }</div>
<div class="line"><a name="l00205"></a><span class="lineno">  205</span>&#160;        <span class="keyword">private</span>:</div>
<div class="line"><a name="l00206"></a><span class="lineno"><a class="line" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a484d28581bfe39a387d90852e38ac36c">  206</a></span>&#160;            Iterator <a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a484d28581bfe39a387d90852e38ac36c">_from</a>;</div>
<div class="line"><a name="l00207"></a><span class="lineno"><a class="line" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a57a42830d3b52d60d2ec1dfd11e2bda6">  207</a></span>&#160;            std::size_t <a class="code" href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a57a42830d3b52d60d2ec1dfd11e2bda6">_size</a>;</div>
<div class="line"><a name="l00208"></a><span class="lineno">  208</span>&#160;        };</div>
<div class="line"><a name="l00209"></a><span class="lineno">  209</span>&#160;    }</div>
<div class="line"><a name="l00210"></a><span class="lineno">  210</span>&#160;}</div>
<div class="line"><a name="l00211"></a><span class="lineno">  211</span>&#160;<span class="preprocessor">#endif // HEAP_SORT</span></div>
<div class="line"><a name="l00212"></a><span class="lineno">  212</span>&#160;</div>
<div class="ttc" id="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap_html_a0535910130c6c750faf7801059e4a9fa"><div class="ttname"><a href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a0535910130c6c750faf7801059e4a9fa">IntroductionToAlgorithm::SortAlgorithm::Sort_Heap::_heapify</a></div><div class="ttdeci">void _heapify(std::size_t elementIndex, CompareType compare=CompareType())</div><div class="ttdoc">_heapify：维持堆性质 </div><div class="ttdef"><b>Definition:</b> <a href="heapsort_8h_source.html#l00102">heapsort.h:102</a></div></div>
<div class="ttc" id="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap_html_a72f5fa2e41901a80fdb9a3bcece8a33f"><div class="ttname"><a href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a72f5fa2e41901a80fdb9a3bcece8a33f">IntroductionToAlgorithm::SortAlgorithm::Sort_Heap::_parentIndex</a></div><div class="ttdeci">std::size_t _parentIndex(std::size_t elementIndex, bool &amp;valid)</div><div class="ttdoc">_parentIndex:返回一个节点的父节点位置 </div><div class="ttdef"><b>Definition:</b> <a href="heapsort_8h_source.html#l00139">heapsort.h:139</a></div></div>
<div class="ttc" id="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap_html"><div class="ttname"><a href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html">IntroductionToAlgorithm::SortAlgorithm::Sort_Heap</a></div><div class="ttdoc">Sort_Heap：用于堆排序的堆，算法导论第6章 </div><div class="ttdef"><b>Definition:</b> <a href="heapsort_8h_source.html#l00040">heapsort.h:40</a></div></div>
<div class="ttc" id="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap_html_a484d28581bfe39a387d90852e38ac36c"><div class="ttname"><a href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a484d28581bfe39a387d90852e38ac36c">IntroductionToAlgorithm::SortAlgorithm::Sort_Heap::_from</a></div><div class="ttdeci">Iterator _from</div><div class="ttdef"><b>Definition:</b> <a href="heapsort_8h_source.html#l00206">heapsort.h:206</a></div></div>
<div class="ttc" id="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap_html_a57a42830d3b52d60d2ec1dfd11e2bda6"><div class="ttname"><a href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a57a42830d3b52d60d2ec1dfd11e2bda6">IntroductionToAlgorithm::SortAlgorithm::Sort_Heap::_size</a></div><div class="ttdeci">std::size_t _size</div><div class="ttdef"><b>Definition:</b> <a href="heapsort_8h_source.html#l00207">heapsort.h:207</a></div></div>
<div class="ttc" id="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap_html_adfaf638a6fd27c6295f7ac0a07484a00"><div class="ttname"><a href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#adfaf638a6fd27c6295f7ac0a07484a00">IntroductionToAlgorithm::SortAlgorithm::Sort_Heap::operator()</a></div><div class="ttdeci">void operator()(const Iterator from, std::size_t size, CompareType compare=CompareType())</div><div class="ttdoc">operator() </div><div class="ttdef"><b>Definition:</b> <a href="heapsort_8h_source.html#l00056">heapsort.h:56</a></div></div>
<div class="ttc" id="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap_html_af4033a3e0cd7dab78cb1081f3dece165"><div class="ttname"><a href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#af4033a3e0cd7dab78cb1081f3dece165">IntroductionToAlgorithm::SortAlgorithm::Sort_Heap::_lchildIndex</a></div><div class="ttdeci">std::size_t _lchildIndex(std::size_t elementIndex, bool &amp;valid)</div><div class="ttdoc">_lchildIndex:返回一个节点的左子节点位置 </div><div class="ttdef"><b>Definition:</b> <a href="heapsort_8h_source.html#l00162">heapsort.h:162</a></div></div>
<div class="ttc" id="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap_html_a64c05fac6da6e6e0d63e733640d0cf97"><div class="ttname"><a href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a64c05fac6da6e6e0d63e733640d0cf97">IntroductionToAlgorithm::SortAlgorithm::Sort_Heap::_setupHeap</a></div><div class="ttdeci">void _setupHeap(CompareType compare=CompareType())</div><div class="ttdoc">_setupHeap:建堆 </div><div class="ttdef"><b>Definition:</b> <a href="heapsort_8h_source.html#l00079">heapsort.h:79</a></div></div>
<div class="ttc" id="namespace_introduction_to_algorithm_html"><div class="ttname"><a href="namespace_introduction_to_algorithm.html">IntroductionToAlgorithm</a></div><div class="ttdoc">Namespace of IntrodunctionToAlgorithm. </div><div class="ttdef"><b>Definition:</b> <a href="longest__common__subsequence_8h_source.html#l00024">longest_common_subsequence.h:24</a></div></div>
<div class="ttc" id="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap_html_a71532dbfd31fdb854aa46dbca6e411ac"><div class="ttname"><a href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a71532dbfd31fdb854aa46dbca6e411ac">IntroductionToAlgorithm::SortAlgorithm::Sort_Heap::_rchildIndex</a></div><div class="ttdeci">std::size_t _rchildIndex(std::size_t elementIndex, bool &amp;valid)</div><div class="ttdoc">_rchildIndex:返回一个节点的右子节点位置 </div><div class="ttdef"><b>Definition:</b> <a href="heapsort_8h_source.html#l00190">heapsort.h:190</a></div></div>
<div class="ttc" id="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap_html_a5af910656f5d3c65d47f992dbf380c61"><div class="ttname"><a href="class_introduction_to_algorithm_1_1_sort_algorithm_1_1_sort___heap.html#a5af910656f5d3c65d47f992dbf380c61">IntroductionToAlgorithm::SortAlgorithm::Sort_Heap::T</a></div><div class="ttdeci">std::iterator_traits&lt; Iterator &gt;::value_type T</div><div class="ttdef"><b>Definition:</b> <a href="heapsort_8h_source.html#l00043">heapsort.h:43</a></div></div>
</div><!-- fragment --></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="dir_8ed9dba4cd616b67da9b1338594e34e0.html">src</a></li><li class="navelem"><a class="el" href="dir_a03788f144befb43ca5cf4ac95d7a30a.html">sort_algorithms</a></li><li class="navelem"><a class="el" href="dir_66d8e1f0ee6c7c79ca7a063f722abe42.html">heap_sort</a></li><li class="navelem"><a class="el" href="heapsort_8h.html">heapsort.h</a></li>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.10 </li>
  </ul>
</div>
</body>
</html>
